package app.netant.book.book365;

import org.jsoup.select.Elements;

import app.netant.book.base.BaseDetail;
/**
 * 
 * @author JiaJ
 *
 */
public class Book365Detail extends BaseDetail {

	public Book365Detail(String netSite) {
		super(netSite);
		this.preHref = "http://www.365book.net";
	}

	//zip
	@Override 
	public void analyseTitle() {
		Elements parts = this.select(".container .row .col-md-9.leftbox .bookd .bookd-title h1");
		if(parts.size()>0){
			String title = parts.get(0).text();
			int idx = title.indexOf("TXT");
			if(idx>0){
				title = title.substring(0,idx).trim();
			}
			System.out.println("【Title】:"+title);
			this.entity.setNovelName(title);
			this.entity.setNovelFullName(title+".zip");
		}
	}

	@Override
	public void analyseTags() {
		Elements parts = this.select(".container .row .col-md-9.leftbox .bookd .clearfix .bookdmore p");
		for(int i=0;i<parts.size();i++){
			String tags = parts.get(i).text();
			int idx = tags.indexOf("：");
			if(idx>0){
				String tagName = tags.substring(0,idx).trim();
				String tagValue = tags.substring(idx+1).trim();
				System.out.println("【Tag】:"+tagName+"="+tagValue);
				this.entity.setTag(tagName, tagValue);
			}
		}
	}

	@Override
	public void analyseMemo() {
		Elements parts = this.select(".container .row .col-md-9.leftbox .bookd .bookdtext p");
		if(parts.size()>0){
			String memo = parts.get(0).text();
			System.out.println("【Memo】:"+memo);
			this.entity.setNovelMemo(memo);
		}
	}

	@Override
	public void analyseHref() {
		Elements parts = this.select(".container .row .col-md-9.leftbox .bookd .bookdtext p a.btn.btn-primary");
		for(int i=0;i<parts.size();i++){
			String text = parts.get(i).text();
			if(text.indexOf("下载")>0){
				String refer = parts.get(i).attr("href");
				if(refer.startsWith("/")){
					refer = this.preHref+refer;
				}
				System.out.println("【Refer】:"+refer);
				this.entity.setRefer(refer);			
				
				Book365Detail detail = new Book365Detail(refer);
				String href = detail.stepHref();
				if(href.startsWith("/")){
					href = this.preHref + href;
				}
				System.out.println("【Href】:"+href);
				this.entity.setDownPath(href);
				
				break;
			}
		}
	}
	
	public String stepHref(){
		
		Elements parts = this.select(".container .down_box .download a");
		for(int i=0;i<parts.size();i++){
			String text = parts.get(i).attr("title");
			if(text!=null && text.indexOf("普通下载")>=0){
				return parts.get(i).attr("href");
			}
		}
		
		return null;
	}
	
	@Override
	public void analyseType() {
		Elements parts = this.select(".container ol.breadcrumb li a");
		if(parts.size()>1){
			String type = parts.get(1).text();
			System.out.println("【Type】:"+type);
			this.entity.setNovelType(type);
		}
	}
	
	public static void main(String[] args) {
		Book365Detail detail = new Book365Detail("http://www.365book.net/book/46887/");
		detail.test();
	}



}
